package com.jp.knowledge.adapter.daily_knowledge;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.jp.knowledge.R;
import com.jp.knowledge.model.DailyKnowledgePackageModel;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.view.JpDiver;
import com.jp.knowledge.view.JpRecycleView;

import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/7/25.
 */

public class DailyKnowledgePackageAdapter extends RecycleVAdapter<DailyKnowledgePackageModel> {

	public DailyKnowledgePackageAdapter(Context context, @NonNull List<DailyKnowledgePackageModel> list) {
		super(context, list);
	}

	@Override
	public int getLayoutId(int viewType) {
		return R.layout.item_daily_knowledge_group;
	}

	@Override
	public void onBind(RecycleVHolder holder, int position) {
		holder.get(R.id.split_space).setVisibility(position == 0 ? View.GONE : View.VISIBLE);
		DailyKnowledgePackageModel data = getData(position);
		holder.setTextView(R.id.item_daily_title, data.getTitle());
		holder.setTextView(R.id.item_daily_desc, data.getTip());
		JpRecycleView recyclerView = holder.get(R.id.item_daily_content_rv);
		if(!recyclerView.hasFixedSize())
			recyclerView.setHasFixedSize(true);
		int direction = data.getDirection();
		RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
		if(layoutManager == null
				|| (direction == 3 && !(layoutManager instanceof GridLayoutManager))
				|| (direction != 3 && (layoutManager instanceof GridLayoutManager))){
			if(direction == 3) {
				recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4){
					@Override
					public boolean canScrollVertically() {
						return false;
					}
				});
			} else {
				recyclerView.setLayoutManager(new LinearLayoutManager(mContext){
					@Override
					public boolean canScrollVertically() {
						return false;
					}
				});
				recyclerView.addItemDecoration(new JpDiver(mContext));
			}
		}
		RecyclerView.Adapter adapter = recyclerView.getAdapter();
		if(adapter == null
				|| (direction == 3 && !(adapter instanceof DailyKnowledgePackageGridAdapter))
				|| (direction != 3 && !(adapter instanceof DailyKnowledgePackageListAdapter))){
			if(direction == 3)
				adapter = new DailyKnowledgePackageGridAdapter(mContext, data.getList());
			else
				adapter = new DailyKnowledgePackageListAdapter(mContext, data.getList(), direction);
			recyclerView.setAdapter(adapter);
		} else {
			if (adapter instanceof DailyKnowledgePackageGridAdapter)
				((DailyKnowledgePackageGridAdapter) adapter).updateAdapter(data.getList());
			if (adapter instanceof DailyKnowledgePackageListAdapter) {
				((DailyKnowledgePackageListAdapter) adapter).setDirection(direction);
				((DailyKnowledgePackageListAdapter) adapter).updateAdapter(data.getList());
			}
		}
	}
}
